package com.icss.common.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.icss.common.util.ConstatFinalUtil;
import com.icss.common.util.RegexUtil;
import com.icss.common.util.VerifyCodeUtils;
import com.icss.common.vo.ApiResponse;
import com.icss.service.IProductDbService;
import com.icss.service.IUsersDbService;
import com.icss.service.impl.ProductDbServiceImpl;
import com.icss.service.impl.UsersDbServiceImpl;
import com.icss.vo.MakeupProduct;

/**
 * 放的是所有公共的代码
 * 
 * @author 陈岩
 */
@WebServlet(urlPatterns = "/CommonServlet")
public class CommonServlet extends BaseServlet
{
	/* 工具类 */
	private RegexUtil regexUtil = new RegexUtil();

	private IProductDbService productDbService;
	private IUsersDbService usersDbService;

	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		UsersDbServiceImpl usersDbServiceImpl = new UsersDbServiceImpl();
		ProductDbServiceImpl productDbServiceImpl = new ProductDbServiceImpl();


		this.productDbService = productDbServiceImpl;
		this.usersDbService = usersDbServiceImpl;
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		System.out.println("==CommonServlet==service=");
		String method = request.getParameter("method");
		if ("code".equalsIgnoreCase(method))
		{
			/* 验证码 */
			this.code(request, response);
		} else if ("uploadFile".equalsIgnoreCase(method))
		{
			/* 打开上传文件 */
			this.uploadFile(request, response);
		} else if ("uploadFileSubmit".equalsIgnoreCase(method))
		{
			/* 上传文件提交 */
			this.uploadFileSubmit(request, response);
		} else if ("uploadFileBack".equalsIgnoreCase(method))
		{
			/* 打开上传文件 */
			this.uploadFileBack(request, response);
		} else if ("uploadFileBackSubmit".equalsIgnoreCase(method))
		{
			/* 上传文件提交 */
			this.uploadFileBackSubmit(request, response);
		}
	}

	/**
	 * 生成一个验证码
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void code(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");

		// 生成随机字串
		/* String verifyCode = VerifyCodeUtil.generateVerifyCode(4); */
		/* 使用自己的类,生成验证码 */
		String verifyCode = this.regexUtil.randStr(4);
		// 存入会话session
		HttpSession session = request.getSession(true);
		/* 将图片中的验证码放到了session中 */
		session.setAttribute("randSess", verifyCode);
		// 生成图片
		int w = 100, h = 40;
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
	}

	/**
	 * 专门用来处理上传图片
	 * 
	 * @return
	 * @throws ServletException
	 */
	public void uploadFile(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/uploadFile.jsp";
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		String fileType = "未知类型";
		String operType = request.getParameter("operType");
		if ("users".equalsIgnoreCase(operType))
		{
			/* 用户上传头像 */
			fileType = "用户头像";
		}
		if (updateFlag == null || "".equalsIgnoreCase(updateFlag))
		{
			/* 清除信息 */
			apiResponse.clearInfo();
		}
		request.setAttribute("fileType", fileType);
		request.setAttribute("response", apiResponse.toJSON());
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 专门用来处理上传图片
	 * 
	 * @return
	 */
	public void uploadFileSubmit(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		String fileFileName = "";
		File file = null;

		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("fileName", fileFileName);
		paramsMap.put("souFile", file);

		/* 搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 相关id */
		String relaId = request.getParameter("relaId");
		/* 类型 */
		String operType = request.getParameter("operType");
		if ("product".equalsIgnoreCase(operType))
		{
			/* 用户上传头像 */
			condMap.clear();
			condMap.put("id", relaId);
			ApiResponse<MakeupProduct> apiResponseUsers = this.productDbService.findOneProductService(condMap);
			MakeupProduct product = apiResponseUsers.getDataOneJava();

			/* 上传图片 */
			apiResponse = this.productDbService.updateOneProductService(product, paramsMap);

		}
		ConstatFinalUtil.SYS_LOGGER.info("上传照片;relaId:{},结果:{}", relaId, apiResponse.toJSON().toJSONString());
		/* 返回结果 */
		this.printOut(apiResponse.toJSON().toJSONString(), response);
	}

	/**
	 * 专门用来处理上传图片
	 * 
	 * @return
	 * @throws ServletException
	 */
	public void uploadFileBack(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/uploadFileBack.jsp";
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		String fileType = "未知类型";
		String operType = request.getParameter("operType");
		if ("users".equalsIgnoreCase(operType))
		{
			/* 用户上传头像 */
			fileType = "用户头像";
		} else if ("board".equalsIgnoreCase(operType))
		{
			/* 用户上传头像 */
			fileType = "板块图片";
		}
		if (updateFlag == null || "".equalsIgnoreCase(updateFlag))
		{
			/* 清除信息 */
			apiResponse.clearInfo();
		}
		request.setAttribute("fileType", fileType);
		request.setAttribute("response", apiResponse.toJSON());
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 专门用来处理上传图片
	 * 
	 * @return
	 */
	public void uploadFileBackSubmit(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 文件上传 */
		Map<String, Object> fileUploadMap = this.fileUploadUtil(request);

		String fileName = fileUploadMap.get("fileName") + "";
		InputStream is = (InputStream) fileUploadMap.get("is");

		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("fileName", fileName);
		paramsMap.put("is", is);

		/* 搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 相关id */
		String relaId = request.getParameter("relaId");
		/* 类型 */
		String operType = request.getParameter("operType");
		if ("product".equalsIgnoreCase(operType))
		{
			/* 用户上传头像 */
			condMap.clear();
			condMap.put("id", relaId);
			ApiResponse<MakeupProduct> apiResponseUsers = this.productDbService.findOneProductService(condMap);
			MakeupProduct product = apiResponseUsers.getDataOneJava();

			/* 上传图片 */
			apiResponse = this.productDbService.updateOneProductService(product, paramsMap);

		}
		ConstatFinalUtil.SYS_LOGGER.info("上传照片;relaId:{},结果:{}", relaId, apiResponse.toJSON().toJSONString());
		/* 返回结果 */
		this.printOut(apiResponse.toJSON().toJSONString(), response);
	}
}
